use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*firefox", 
                                    browser_url => "http://localhost.com" );

$sel->open_ok("/");
#$sel->click_ok("log_in_top");
$sel->wait_for_page_to_load_ok("30000");
$sel->is_element_present_ok("link=Log out");
$sel->click_ok("link=New");
$sel->wait_for_page_to_load_ok("30000");
$sel->text_is("link=SeleniumTest", "SeleniumTest");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_text_present("Used by Selenium tests.") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->click_ok("link=SeleniumTest");
$sel->wait_for_page_to_load_ok("30000");
$sel->select_ok("bug_severity", "label=blocker");
$sel->type_ok("short_desc", "Testing a new bug submit.");
$sel->type_ok("comment", "We are going to test a new bug submit.\n\nLet's see if it works...");
$sel->click_ok("commit");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("alertCancel") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//div[\@id='severityAlert']/p[1]", "A suggestion for the severity of your bug has been made to help you, please check if the suggested severity is correct.");
$sel->text_is("//div[\@id='severityAlert']/p[2]/b", "Your severity:");
$sel->text_is("//div[\@id='severityAlert']/p[3]/b", "Suggested severity:");
$sel->text_is("//div[\@id='severityAlert']/p[2]", "Your severity:\nblocker");
$sel->text_is("//div[\@id='severityAlert']/p[3]", "Suggested severity:\nenhancement");
$sel->click_ok("alertAgree");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_text_present("has been added to the database") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->is_text_present_ok("Testing a new bug submit.");
$sel->is_text_present_ok("We are going to test a new bug submit.\n\nLet's see if it works...");
$sel->click_ok("link=Administration");
$sel->wait_for_page_to_load_ok("30000");
$sel->click_ok("link=Bug Prediction");
$sel->text_is("link=Statistics", "Statistics");
$sel->click_ok("link=Statistics");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("//td[\@id='content']/table") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/h3", "Statistics");
$sel->text_is("//td[\@id='content']/p", "View statistics for the latest 100 bug submits.");
$sel->table_is("//td[\@id='content']/table.1.0", "Testing a new bug submit.");
$sel->table_is("//td[\@id='content']/table.1.1", "SeleniumTest");
$sel->table_is("//td[\@id='content']/table.1.2", "SeleniumTest");
$sel->table_is("//td[\@id='content']/table.1.4", "blocker");
$sel->table_is("//td[\@id='content']/table.1.5", "enhancement");
$sel->table_is("//td[\@id='content']/table.1.6", "enhancement");
